-- region *.lua
-- Date
-- 此文件由[BabeLua]插件自动生成

wifi.setmode(wifi.STATION)
wifi.sta.config("TP-LINK_JBNS", "jbnsbmns")


led1 = 0
-- led2 = 4
gpio.mode(led1, gpio.OUTPUT)
-- gpio.mode(led2, gpio.OUTPUT)
gpio.write(led1, gpio.HIGH)
-- gpio.write(led2,gpio.HIGH)
-- gpio.write(led1,gpio.LOW)
-- gpio.write(led2,gpio.LOW)



APIKEY = "55a60a0791c050ab78d3f5338518f86c"


tmr.alarm(1, 1000, 1, function()
    if (wifi.sta.getip() ~= nil) then
        tmr.stop(1)
        print("Config done, IP is " .. wifi.sta.getip())

        sv = net.createServer(net.TCP, 120)

        sv:listen(90, function(c)
            gpio.write(led1, gpio.LOW)
            tmr.alarm(0, 500, tmr.ALARM_SINGLE, function() gpio.write(led1, gpio.HIGH) end)


            c:on("receive", function(c, pl)
                print("RCV:" .. pl)
                http.get("http://www.tuling123.com/openapi/api?key=" .. APIKEY .. "&info=" .. pl, nil, function(code, data)
                    if (code < 0) then
                        str = "HTTP request failed " .. code
                    else
                        str = code .. " " .. data
                    end
                    print(str)
                    c:send(str)

                end )

                --
            end )

        end )
    end
end )
-- endregion
